/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-06 15:05:45  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.bp.service.impl;

import com.google.common.collect.Lists;
import com.joysuch.wwyt.bp.bean.BpWorkSafetyApplyUserDetailBean;
import com.joysuch.wwyt.bp.bean.safetyworkcert.BpWorkSafetyCertDto;
import com.joysuch.wwyt.bp.entity.BpWorkSafetyApplyUser;
import com.joysuch.wwyt.bp.enums.SafetyWorkTypes;
import com.joysuch.wwyt.bp.enums.UserTypes;
import com.joysuch.wwyt.bp.repository.BpWorkSafetyApplyUserDao;
import com.joysuch.wwyt.bp.service.BpWorkSafetyApplyUserService;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.entity.BaseUser;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class BpWorkSafetyApplyUserServiceImpl implements BpWorkSafetyApplyUserService {

    @Autowired
    private BpWorkSafetyApplyUserDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private BaseUserDao baseUserDao;

    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        // 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {

    }

    @Override
    public BpWorkSafetyApplyUser findById(Long id) {
        return dao.findById(id).get();
    }

    @Override
    public List<BpWorkSafetyApplyUserDetailBean> findUsersByCertTypeAndWorkId(SafetyWorkTypes type, Long workId) {
        List<BpWorkSafetyApplyUser> users = dao.findByTypeAndWorkId(type.getName(), workId);
        return PageDataUtils.convertListData(users,
                new PageDataConvert<BpWorkSafetyApplyUserDetailBean, BpWorkSafetyApplyUser>() {

                    @Override
                    public BpWorkSafetyApplyUserDetailBean convert(BpWorkSafetyApplyUser f) {
                        UserTypes userType = UserTypes.findByName(f.getUserType());
                        if (userType == UserTypes.CONTRACTOR) {
                            // 承包商用户
                            return findContractorUser(f);
                        } else if (userType == UserTypes.STAFF) {
                            // 系统内部用户
                            return findSystemStaff(f);
                        } else {
                            throw new IllegalStateException("全流程-安全作业证-未知的人员类型");
                        }
                    }
                });
    }

    protected BpWorkSafetyApplyUserDetailBean findSystemStaff(BpWorkSafetyApplyUser f) {
        BaseUser user = baseUserDao.findById(f.getOpUserId()).get();
        BpWorkSafetyApplyUserDetailBean bean = new BpWorkSafetyApplyUserDetailBean();
        List<BpWorkSafetyCertDto> certNames = Lists.newArrayList();
        certNames.add(new BpWorkSafetyCertDto(f.getCertId(),f.getCertName()));
        bean.setCertNames(certNames);
        bean.setDepartName(f.getDepartName());
        // 此处是为了兼容前端，前端添加、修改使用了同一套东西
        // 必须给前端返回 IN_${user_id形式}
        bean.setId(UserTypes.STAFF.getCode() + "_" + f.getOpUserId());
        bean.setOpUserName(user.getRealName());
        bean.setOpUserId(f.getOpUserId());
        bean.setUserType(f.getUserType());
        return bean;
    }

    protected BpWorkSafetyApplyUserDetailBean findContractorUser(BpWorkSafetyApplyUser f) {
        BpWorkSafetyApplyUserDetailBean bean = new BpWorkSafetyApplyUserDetailBean();
        List<BpWorkSafetyCertDto> certNames = Lists.newArrayList();
        certNames.add(new BpWorkSafetyCertDto(f.getCertId(),f.getCertName()));
        bean.setCertNames(certNames);
        bean.setDepartName(f.getDepartName());
        bean.setId(UserTypes.CONTRACTOR.getCode() + "_" + f.getOpUserId());
        // TODO 设置承包商人员信息
        // 此处是为了兼容前端，前端添加、修改使用了同一套东西
        // 必须给前端返回 IN_${user_id形式}
        // bean.setOpUserName(user.getRealName());
        bean.setOpUserId(f.getOpUserId());
        bean.setUserType(f.getUserType());
        return bean;
    }

}
